<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入小区名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="地区" prop="advert_place_id">

        <el-select v-model="queryParams.province_id" placeholder="请选择省" @change="sxprofuc">
          <el-option v-for="item in Area" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>

        <el-select v-model="queryParams.city_id" placeholder="请选择市" style="margin-left: 20px;" @change="sxcitfuc">
          <el-option v-for="item in cityQuery" :key="item.value" :label="item.name" :value="item.id">
          </el-option>
        </el-select>

        <el-select v-model="queryParams.area_id" placeholder="请选择区" style="margin-left: 20px;">
          <el-option v-for="item in areaQuery" :key="item.value" :label="item.name" :value="item.id">
          </el-option>
        </el-select>

      </el-form-item>




      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:post:add']">新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:post:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="DataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />


      <el-table-column label="ID" width="50" align="center" prop="ID">
        <template slot-scope="scope">
          {{scope.row.id}}
        </template>
      </el-table-column>

      <el-table-column label="小区名" width="200" align="center" prop="name">
        <template slot-scope="scope">
          {{scope.row.name}}
        </template>
      </el-table-column>

      <el-table-column label="小区图片" align="center" prop="thumb" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <img :src="scope.row.thumb" width="50" />
        </template>
      </el-table-column>

      <el-table-column label="修建时间" align="center" prop="construct_time" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.construct_time}}
        </template>
      </el-table-column>

      <el-table-column label="入住时间" align="center" prop="occy_time" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.occy_time}}
        </template>
      </el-table-column>

      <el-table-column label="面积" align="center" prop="acreage" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.acreage}}
        </template>
      </el-table-column>

      <el-table-column label="住户" align="center" prop="household_num" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.household_num}}
        </template>
      </el-table-column>

      <el-table-column label="商铺" align="center" prop="shop_num" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.shop_num}}
        </template>
      </el-table-column>

      <el-table-column label="车库" align="center" prop="garage_num" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.garage_num}}
        </template>
      </el-table-column>

      <el-table-column label="省" align="center" prop="province_name" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.province_name}}
        </template>
      </el-table-column>

      <el-table-column label="市" align="center" prop="city_name" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.city_name}}
        </template>
      </el-table-column>

      <el-table-column label="区" align="center" prop="district_name" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.district_name}}
        </template>
      </el-table-column>

      <el-table-column label="详细地址" align="center" prop="address" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.address}}
        </template>
      </el-table-column>



      <el-table-column label="状态" align="center" prop="state" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.state" />
        </template>
      </el-table-column>



      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加开始 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" label-width="120px" :rules="rules" style="height: 800px;overflow-y: scroll;">

        <el-form-item label="名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入名字" clearable size="small" />
        </el-form-item>


        <el-form-item label="分区" prop="partition">
          <el-tag :key="tag" v-for="tag in partitionTags" closable :disable-transitions="false"
            @close="handleClosePartition(tag)" style="margin-right: 10px;">
            {{tag}}
          </el-tag>
          <el-input class="input-new-tag" v-if="partitionVisible" v-model="partitionValue" ref="saveTagInputPartition"
            size="small" @keyup.enter.native="handleInputConfirmPartition" @blur="handleInputConfirmPartition">
          </el-input>
          <el-button v-else class="button-new-tag" size="small" @click="showPartition">+ 新增</el-button>
        </el-form-item>


        <el-form-item label="楼号" prop="building">
          <el-tag :key="tag" v-for="tag in buildingTags" closable :disable-transitions="false"
            @close="handleCloseBuilding(tag)" style="margin-right: 10px;">
            {{tag}}
          </el-tag>
          <el-input class="input-new-tag" v-if="buildingVisible" v-model="buildingValue" ref="saveTagInputBuilding"
            size="small" @keyup.enter.native="handleInputConfirmBuilding" @blur="handleInputConfirmBuilding">
          </el-input>
          <el-button v-else class="button-new-tag" size="small" @click="showBuilding">+ 新增</el-button>
        </el-form-item>

        <el-form-item label="楼层" prop="floor">
          <el-tag :key="tag" v-for="tag in floorTags" closable :disable-transitions="false"
            @close="handleCloseFloor(tag)" style="margin-right: 10px;">
            {{tag}}
          </el-tag>
          <el-input class="input-new-tag" v-if="floorVisible" v-model="floorValue" ref="saveTagInputFloor" size="small"
            @keyup.enter.native="handleInputConfirmFloor" @blur="handleInputConfirmFloor">
          </el-input>
          <el-button v-else class="button-new-tag" size="small" @click="showFloor">+ 新增</el-button>
        </el-form-item>


        <el-form-item label="门号" prop="hnum">
          <el-tag :key="tag" v-for="tag in hnumTags" closable :disable-transitions="false" @close="handleCloseHnum(tag)"
            style="margin-right: 10px;">
            {{tag}}
          </el-tag>
          <el-input class="input-new-tag" v-if="hnumVisible" v-model="hnumValue" ref="saveTagInputHnum" size="small"
            @keyup.enter.native="handleInputConfirmHnum" @blur="handleInputConfirmHnum">
          </el-input>
          <el-button v-else class="button-new-tag" size="small" @click="showHnum">+ 新增</el-button>
        </el-form-item>


        <el-form-item label="图片" prop="thumb">
          <el-upload :action="upload_url" :headers="headers" :before-upload="handleSetHeaders"
            :on-success="handleFileSuccess" :show-file-list="false" class="avatar-uploader">
            <img v-if="form.thumb!==undefined" :src="form.thumb" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="修建时间" prop="construct_time">
          <el-date-picker v-model="form.construct_time" type="date" placeholder="选择日期" format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="入住时间" prop="occy_time">
          <el-date-picker v-model="form.occy_time" type="date" placeholder="选择日期" format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>


        <el-form-item label="面积" prop="acreage">
          <el-input v-model="form.acreage" placeholder="请输入面积" clearable size="small" />
        </el-form-item>

        <el-form-item label="住户" prop="household_num">
          <el-input-number v-model="form.household_num" :precision="0" :min="1" label="住户"></el-input-number>
        </el-form-item>

        <el-form-item label="商铺" prop="shop_num">
          <el-input-number v-model="form.shop_num" :precision="0" :min="1" label="商铺"></el-input-number>
        </el-form-item>

        <el-form-item label="车库" prop="garage_num">
          <el-input-number v-model="form.garage_num" :precision="0" :min="1" label="车库"></el-input-number>
        </el-form-item>

        <el-form-item label="简介" prop="introductory">
          <Editor v-model="form.introductory" :minHeight="400" placeholder="请输入简介" clearable size="small" />
        </el-form-item>

        <el-form-item label="地区" prop="province_id">

          <el-select v-model="province" placeholder="请选择省" @change="provinceFuc">
            <el-option v-for="item in Area" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>

          <el-select v-model="city" placeholder="请选择市" style="margin-left: 20px;" @change="cityFuc">
            <el-option v-for="item in cityQuery" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>

          <el-select v-model="area" placeholder="请选择区" style="margin-left: 20px;" @change="areaFuc">
            <el-option v-for="item in areaQuery" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>

        </el-form-item>

        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" clearable size="small" />
        </el-form-item>


        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
              :label="dict.value">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加结束 -->
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
  import * as Advert from "@/api/operation/advert";
  import * as Community from "@/api/operation/community";
  import * as District from "@/api/operation/district";
  import * as AdvertPlace from "@/api/operation/advert_place";
  import * as Auth from '@/utils/auth'

  export default {
    name: "Advert",
    dicts: ['sys_normal_disable'],
    data() {
      return {

        upload_url: process.env.VUE_APP_BASE_URL + "/file/upload",
        headers: Auth.getHeader(),
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        open: false,
        form: {},

        title: "",
        // 日期范围
        dateRange: [],
        DataList: [],
        // 查询参数
        queryParams: {
          page: 1,
          pageSize: 10
        },

        queryDistrictParams: {
          page: 1,
          pageSize: 100,
          pid: 0
        },
        dialogVisible: false,
        dialogImageUrl: undefined,
        PlaceList: [],


        // 表单校验
        rules: {
          name: [{
            required: true,
            message: "小区名字不能为空",
            trigger: "blur"
          }],
          thumb: [{
            required: true,
            message: "小区图片不能为空",
            trigger: "blur"
          }],
          construct_time: [{
            required: true,
            message: "小区修建时间不能为空",
            trigger: "blur"
          }],
          occy_time: [{
            required: true,
            message: "小区入住时间不能为空",
            trigger: "blur"
          }],
          acreage: [{
            required: true,
            message: "小区面积不能为空",
            trigger: "blur"
          }],
          household_num: [{
            required: true,
            message: "小区住户不能为空",
            trigger: "blur"
          }],
          shop_num: [{
            required: true,
            message: "小区商铺不能为空",
            trigger: "blur"
          }],
          garage_num: [{
            required: true,
            message: "小区车库不能为空",
            trigger: "blur"
          }],
          introductory: [{
            required: true,
            message: "小区简介不能为空",
            trigger: "blur"
          }],
          address: [{
            required: true,
            message: "小区详细地址不能为空",
            trigger: "blur"
          }],
          state: [{
            required: true,
            message: "状态不能为空",
            trigger: "blur"
          }],

          province_id: [{
            required: true,
            message: "地区不能为空",
            trigger: "change"
          }],

        },
        Area: [],
        cityQuery: [],
        areaQuery: [],

        province: null,
        city: null,
        area: null,

        partitionTags: [], //分区A B C
        partitionVisible: false,
        partitionValue: '',


        buildingTags: [], //楼栋1 2
        buildingVisible: false,
        buildingValue: '',


        floorTags: [], //楼层 1 2
        floorVisible: false,
        floorValue: '',


        hnumTags: [], //门号 1 2
        hnumVisible: false,
        hnumValue: '',


      };
    },

    watch: {

    },
    created() {
      this.getList();
      this.getProvinceList();

    },
    methods: {


      handleClosePartition(tag) {
        this.partitionTags.splice(this.partitionTags.indexOf(tag), 1);
      },

      showPartition() {
        this.partitionVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveTagInputPartition.$refs.input.focus();
        });
      },

      handleInputConfirmPartition() {
        let partitionValue = this.partitionValue;
        if (partitionValue) {
          if(!this.partitionTags){
            this.partitionTags = [];
          }
          this.partitionTags.push(partitionValue);
        }
        this.partitionVisible = false;
        this.partitionValue = '';
      },


      //楼号
      handleCloseBuilding(tag) {
        this.buildingTags.splice(this.buildingTags.indexOf(tag), 1);
      },

      showBuilding() {
        this.buildingVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveTagInputBuilding.$refs.input.focus();
        });
      },

      handleInputConfirmBuilding() {
        let buildingValue = this.buildingValue;
        if (buildingValue) {
          if(!this.buildingTags){
            this.buildingTags = [];
          }
          this.buildingTags.push(buildingValue);
        }
        this.buildingVisible = false;
        this.buildingValue = '';
      },


      //楼层++++++++++++++++
      handleCloseFloor(tag) {
        this.floorTags.splice(this.floorTags.indexOf(tag), 1);
      },

      showFloor() {
        this.floorVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveTagInputFloor.$refs.input.focus();
        });
      },

      handleInputConfirmFloor() {
        let floorValue = this.floorValue;
        if (floorValue) {
          if (!this.floorTags) {
            this.floorTags = [];
          }
          this.floorTags.push(floorValue);
        }
        this.floorVisible = false;
        this.floorValue = '';
      },


      //门号++++++++++++++++
      handleCloseHnum(tag) {
        this.hnumTags.splice(this.hnumTags.indexOf(tag), 1);
      },

      showHnum() {
        this.hnumVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveTagInputHnum.$refs.input.focus();
        });
      },

      handleInputConfirmHnum() {
        let hnumValue = this.hnumValue;
        if (hnumValue) {
          if (!this.hnumTags) {
            this.hnumTags = [];
          }
          this.hnumTags.push(hnumValue);
        }
        this.hnumVisible = false;
        this.hnumValue = '';
      },




      provinceFuc() {
        this.cityQuery = [],
          this.areaQuery = [],
          this.city = '',
          this.area = ""
        this.$set(this.form, 'province_id', this.province);
        this.getCityList(this.province);
        this.Area.forEach((item) => {
          if (this.province === item.id) {
            this.$set(this.form, 'province_name', item.name);
          }
        })
      },



      cityFuc() {
        this.areaQuery = [],
          this.area = ""
        this.$set(this.form, 'city_id', this.city);
        this.getAreaList(this.city);
        this.cityQuery.forEach((item) => {
          if (this.city === item.id) {
            this.$set(this.form, 'city_name', item.name);
          }
        })
      },

      areaFuc() {
        this.$set(this.form, 'district_id', this.area);
        this.areaQuery.forEach((item) => {
          if (this.area === item.id) {
            this.$set(this.form, 'district_name', item.name);
          }
        })
      },

      sxprofuc() {
        this.getCityList(this.queryParams.province_id);
        this.areaQuery = [];
        this.$set(this.queryParams, 'city', '');
        this.$set(this.queryParams, 'area', '');
      },

      sxcitfuc() {
        this.getAreaList(this.queryParams.city_id);
        this.$set(this.queryParams, 'area', '');
      },



      /** 查询小区列表 */
      getList() {
        this.loading = true;
        Community.List(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.DataList = response.data.rows;
          this.total = response.data.total;
          this.loading = false;
        });
      },


      /** 查询省 */
      getProvinceList() {
        this.loading = true;
        District.List().then(response => {
          this.Area = response.data.rows;
          console.log('Area', this.Area);
          this.total = response.data.total;
          this.loading = false;
        });
      },

      getCityList(pid) {
        this.loading = true;
        var pid = {
          pid: pid
        }
        District.List(pid).then(response => {
          this.cityQuery = response.data.rows;
          console.log('cityQuery', this.cityQuery);
          this.total = response.data.total;
          this.loading = false;
        });
      },



      getAreaList(pid) {
        this.loading = true;
        var pid = {
          pid: pid
        }
        District.List(pid).then(response => {
          this.areaQuery = response.data.rows;
          console.log('areaQuery', this.areaQuery);
          this.total = response.data.total;
          this.loading = false;
        });
      },

      // 表单重置
      reset() {
        this.form = {};
        this.partitionTags = [];
        this.buildingTags = [];
        this.floorTags = [];
        this.hnumTags = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.queryParams = {
          page: 1,
          pageSize: 10
        };
        this.handleQuery();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加小区";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const Id = row.id || this.ids
        Community.Show(Id).then(response => {

          this.partitionTags = response.data.partition;
          this.buildingTags = response.data.building;
          this.floorTags = response.data.floor;
          this.hnumTags = response.data.hnum;

          this.form = response.data;
          this.getAreaList(response.data.city_id);
          this.getCityList(response.data.province_id)
          setTimeout(() => {
            this.province = response.data.province_id
            this.city = response.data.city_id
            this.area = response.data.district_id
          }, 100);
          this.open = true;
          this.title = "修改小区";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        if (this.partitionTags && this.partitionTags.length > 0) {
          this.$set(this.form, 'partition', this.partitionTags);
        }

        if (this.buildingTags && this.buildingTags.length > 0) {
          this.$set(this.form, 'building', this.buildingTags);
        }

        if (this.floorTags && this.floorTags.length > 0) {
          this.$set(this.form, 'floor', this.floorTags);
        }

        if (this.hnumTags && this.hnumTags.length > 0) {
          this.$set(this.form, 'hnum', this.hnumTags);
        }

        console.log('表单数据', this.form);
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              Community.Update(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              Community.Add(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const Id = row.id || this.ids;
        this.$modal.confirm('是否确认删除编号为"' + Id + '"的数据项？').then(function() {
          return Community.Delete(Id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      handleSetHeaders() {
        this.headers = Auth.getHeader();
      },
      /****文件上传*****/
      handleFileSuccess(res) {

        // this.form.thumb = res.data;
        this.$set(this.form, 'thumb', res.data);

      }
    },
  };
</script>
<style>
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }

  .avatar-uploader {
    width: 100px;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }

  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
